<template>
  <div class="email-verification-container">
    <!-- 添加Header组件 -->
    <Header />
    
    <!-- 主内容区域 -->
    <div class="verification-content">
      <!-- 头部Logo -->
      <div class="header-logo">
        <img src="../../assets/images/login/logo2.png" class="logo-image" />
        <span class="logo-text">{{ $t('login.logoText') }}</span>
      </div>
      
      <!-- 标题 -->
      <h1 class="verification-title">{{ $t('verifyEmail.title') }}</h1>
      
      <!-- 验证信息框 -->
      <div class="verification-box">
        <div class="verification-message">
          <p class="message-line">
            {{ $t('verifyEmail.sentToEmail1') }}
            <span class="email-highlight">{{ userEmail }}</span>
            {{ $t('verifyEmail.sentToEmail2') }}
          </p>
          <p class="message-line">
            {{ $t('verifyEmail.clickLink') }}
          </p>
          <p class="privacy-notice">
            {{ $t('verifyEmail.privacyNotice') }}
          </p>
        </div>
      </div>
      
      <!-- 按钮区域 -->
      <div class="button-container">
        <el-button @click="goBack" class="back-btn">{{ $t('common.back') }}</el-button>
        <el-button @click="verificationComplete" :loading="isLoading" class="complete-btn">
          {{ isLoading ? $t('verifyEmail.verifying') : $t('verifyEmail.complete') }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { userApi } from '@/api';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
import Header from '../../components/layout/Header.vue';

export default {
  name: 'EmailVerification',
  components: {
    Header
  },
  setup() {
    const router = useRouter();
    const route = useRoute();
    const { t } = useI18n();
    const userEmail = ref('');
    const isLoading = ref(false);
    
    onMounted(() => {
      // 从路由参数或localStorage获取邮箱
      userEmail.value = route.query.email || localStorage.getItem('tempUserEmail') || 'user@example.com';
    });
    
    const goBack = () => {
      // 返回统一的注册页面
      router.push('/register');
    };
    
    const verificationComplete = async () => {
      isLoading.value = true;
      try {
        const userType = route.query.userType || localStorage.getItem('tempUserType') || 'student';
        const email = route.query.email || localStorage.getItem('tempUserEmail');
        
        if (!email) {
          ElMessage.error(t('verifyEmail.emailNotExist'));
          return;
        }
        
        // 验证邮箱格式
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
          ElMessage.error(t('verifyEmail.invalidEmailFormat'));
          return;
        }
        
        // 调用新的邮箱状态检查接口
        const checkData = {
          userType: userType,
          email: email
        };
        
        const result = await userApi.checkEmailStatus(checkData);
        
        if (result.code === 200) {
          if (result.isVerified) {
            ElMessage.success(result.message || t('verifyEmail.success'));
            
            // 清除临时存储的注册信息
            localStorage.removeItem('tempUserEmail');
            localStorage.removeItem('tempUserType');
            localStorage.removeItem('tempUserPassword');
            localStorage.removeItem('tempUserConfirmPassword');
            
            // 跳转到对应的登录页面
            router.push('/login');
          } else {
            ElMessage.warning(t('verifyEmail.notActivated'));
          }
        } else {
          ElMessage.error(result.message || t('verifyEmail.verificationFailed'));
        }
      } catch (error) {
        console.error('验证失败:', error);
        console.error('错误详情:', error.response?.data);
        console.error('错误状态码:', error.response?.status);
        
        // 根据不同的错误类型显示不同的提示信息
        if (error.response) {
          const { status, data } = error.response;
          
          console.log('错误状态码:', status);
          console.log('错误数据:', data);
          
          switch (status) {
            case 400:
              ElMessage.error(t('verifyEmail.invalidRequest'));
              break;
            case 404:
              ElMessage.error(t('verifyEmail.emailNotFound'));
              break;
            case 422:
              let errorMessage = t('verifyEmail.invalidEmailFormat');
              
              if (data) {
                if (typeof data === 'string') {
                  errorMessage = data;
                } else if (typeof data === 'object') {
                  errorMessage = data.message || data.error || data.msg || JSON.stringify(data);
                }
              }
              ElMessage.error(errorMessage);
              break;
            case 500:
              ElMessage.error(t('verifyEmail.serverError'));
              break;
            default:
              ElMessage.error(t('verifyEmail.verificationFailed'));
          }
        } else if (error.request) {
          ElMessage.error(t('verifyEmail.networkError'));
        } else {
          ElMessage.error(t('verifyEmail.verificationFailed'));
        }
      } finally {
        isLoading.value = false;
      }
    };
    
    return {
      userEmail,
      isLoading,
      goBack,
      verificationComplete
    };
  }
};
</script>

<style scoped>
.email-verification-container {
  min-height: 100vh;
  width: 100vw;
  position: relative;
  overflow: hidden;
  background: #fff;
}

.verification-content {
  position: relative;
  z-index: 2;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: #fff;
}

.header-logo {
  position: absolute;
  top: 32px;
  left: 48px;
  display: flex;
  align-items: center;
}

.logo-image {
  width: 48px;
  height: 48px;
  margin-right: 12px;
}

.logo-text {
  font-family: "华文隶书", "STLiti", serif;
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.verification-title {
  font-size: 36px;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 40px;
  margin-top: 60px;
}

.verification-box {
  background: #e6f3ff;
  border-radius: 16px;
  padding: 40px;
  max-width: 600px;
  width: 100%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  margin-bottom: 40px;
}

.verification-message {
  text-align: center;
}

.message-line {
  font-size: 18px;
  color: #333;
  margin-bottom: 16px;
  line-height: 1.6;
}

.email-highlight {
  color: #409eff;
  font-weight: bold;
  font-size: 20px;
}

.privacy-notice {
  font-size: 16px;
  color: #666;
  margin-top: 24px;
  line-height: 1.5;
  font-style: italic;
}

.button-container {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 350px;
  margin-top: 20px;
}

.back-btn {
  width: 120px;
  height: 48px;
  background: #f5f5f5;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  color: #333;
  transition: all 0.3s ease;
}

.complete-btn {
  width: 120px;
  height: 48px;
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  color: white;
  transition: all 0.3s ease;
}

.back-btn:hover {
  background: #e0e0e0;
  color: #409eff;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.complete-btn:hover {
  background: linear-gradient(135deg, #e55a2b 0%, #e0851a 100%);
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(255, 107, 53, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .verification-content {
    padding: 10px;
  }
  
  .header-logo {
    top: 20px;
    left: 20px;
  }
  
  .logo-text {
    font-size: 20px;
  }
  
  .verification-title {
    font-size: 28px;
    margin-top: 40px;
  }
  
  .verification-box {
    padding: 30px 20px;
    margin: 0 10px 30px 10px;
  }
  
  .message-line {
    font-size: 16px;
  }
  
  .email-highlight {
    font-size: 18px;
  }
  
  .privacy-notice {
    font-size: 14px;
  }
  
  .button-container {
    flex-direction: column;
    gap: 16px;
  }
  
  .back-btn,
  .complete-btn {
    width: 200px;
  }
}
</style> 